using UnityEngine;

[ExecuteInEditMode]
public class ControllerCheckPoint : ControllerGeneric
{
	public float length = 1f;

	public float width = 1f;

	public float height = 1f;

	public GameObject triggerBox;

	public Object controllerSpawnPrefab;

	public GameObject spawnPoint;

	public int checkPointNumber = -1;

	private bool staticState;

	public override void OnLevelInPlace(Transform levelTransform)
	{
		base.OnLevelInPlace(levelTransform);
		if (levelTransform == base.transform.root)
		{
			PrepareForGame();
		}
	}

	public void PrepareForGame()
	{
		SetNames();
		SetParenting();
		SetStatic();
		CheckPointOnContact component = triggerBox.GetComponent<CheckPointOnContact>();
		component.spawnPointObject = new GameObject("generated temporary spawn point");
		component.spawnPointObject.transform.parent = triggerBox.transform;
		component.spawnPointObject.transform.position = spawnPoint.transform.position;
		component.spawnPointObject.transform.rotation = spawnPoint.transform.rotation;
		component.checkPointNumber = checkPointNumber;
		if (Application.isPlaying)
		{
			Object.Destroy(spawnPoint);
			Object.Destroy(base.gameObject);
		}
		else
		{
			Object.DestroyImmediate(spawnPoint);
			Object.DestroyImmediate(base.gameObject);
		}
	}

	private void Update()
	{
		if (!Application.isEditor || Application.isPlaying)
		{
			return;
		}
		SetupRemoteControl();
		if (IsRemoteControlActive())
		{
			base.transform.position = remoteControl.transform.position;
			base.transform.rotation = remoteControl.transform.rotation;
			base.transform.localScale = remoteControl.transform.localScale;
			length = remoteControl.transform.localScale.z;
			width = remoteControl.transform.localScale.x;
			height = remoteControl.transform.localScale.y;
		}
		if (length < 0.0001f)
		{
			length = 0.0001f;
		}
		if (width < 0.0001f)
		{
			width = 0.0001f;
		}
		if (height < 0.0001f)
		{
			height = 0.0001f;
		}
		if (remoteControl != null)
		{
			if (remoteControl.manualScale)
			{
				remoteControl.scale = new Vector3(remoteControl.scale.x, remoteControl.scale.y, remoteControl.scale.z);
			}
			else
			{
				remoteControl.transform.localScale = new Vector3(width, height, length);
			}
		}
		Setup();
	}

	private void Setup()
	{
		AssureParenting();
		SetNames();
		triggerBox.transform.localPosition = Vector3.zero;
		triggerBox.transform.localEulerAngles = Vector3.zero;
		triggerBox.transform.localScale = Vector3.one;
	}

	private void SetStatic(bool newStaticState = true)
	{
		if (newStaticState != staticState)
		{
			staticState = newStaticState;
			triggerBox.isStatic = staticState;
		}
	}

	private void SetParenting()
	{
		for (int childCount = base.transform.childCount; childCount > 0; childCount = base.transform.childCount)
		{
			base.transform.GetChild(0).parent = base.transform.parent;
		}
	}

	private void SetNames()
	{
		base.name = "CP";
		triggerBox.name = "Checkpoint";
		if (checkPointNumber != -1)
		{
			base.name = base.name + "  " + checkPointNumber;
			GameObject obj = triggerBox;
			obj.name = obj.name + "  " + checkPointNumber;
		}
	}

	public void SetupRemoteControl()
	{
		if (remoteControl == null)
		{
			InitRemoteControl();
			remoteControl.gameObject.name = remoteControl.gameObject.name + base.name;
			remoteControl.gameObject.transform.position = base.transform.position;
			remoteControl.gameObject.transform.rotation = base.transform.rotation;
			remoteControl.gameObject.transform.localScale = new Vector3(width, height, length);
			PresetsCheckPoint presetsCheckPoint = remoteControl.gameObject.AddComponent<PresetsCheckPoint>();
			presetsCheckPoint.SetupControllerSpawn(controllerSpawnPrefab);
			presetsCheckPoint.controllerSpawn.remoteControl.SetPlacementParent(presetsCheckPoint.controllerSpawn.platformRC);
			presetsCheckPoint.controllerSpawn.remoteControl.alignX = BoxAlignment.Center;
			presetsCheckPoint.controllerSpawn.remoteControl.alignY = BoxAlignment.LeftIn;
			presetsCheckPoint.controllerSpawn.remoteControl.alignZ = BoxAlignment.Center;
		}
	}

	public override void CopyFromRemoteControl(RemoteControl rc)
	{
		base.CopyFromRemoteControl(rc);
		CopyParentingFromRemoteControl(rc);
		PresetsCheckPoint component = rc.gameObject.GetComponent<PresetsCheckPoint>();
		PresetsCheckPoint component2 = remoteControl.gameObject.GetComponent<PresetsCheckPoint>();
		component2.controllerSpawn.remoteControl.alignX = BoxAlignment.None;
		component2.controllerSpawn.remoteControl.alignY = BoxAlignment.None;
		component2.controllerSpawn.remoteControl.alignZ = BoxAlignment.None;
		component2.controllerSpawn.remoteControl.SetPosition(component.controllerSpawn.transform.position);
		component2.controllerSpawn.remoteControl.transform.rotation = component.controllerSpawn.transform.rotation;
	}
}
